//
//  YCMutablePhotoItemView.m
//  YCSchool
//
//  Created by gmall on 16/5/7.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  九宫格展示图片

#import "YCMutablePhotoItemView.h"

//二维码相关，长按图片处理
#import <ZXingObjC/ZXMultiFormatReader.h>
#import <ZXingObjC/ZXCGImageLuminanceSource.h>
#import <ZXingObjC/ZXHybridBinarizer.h>
#import <ZXingObjC/ZXBinaryBitmap.h>
#import <ZXingObjC/ZXDecodeHints.h>
#import <ZXingObjC/ZXResult.h>


#pragma mark - XHImageViewer(GO)

@interface XHImageViewer(GO)
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSArray *imgViews;
- (UIImageView *)currentView;//写上，会调用父类的方法
@end
@implementation XHImageViewer(GO)
@end

#pragma mark - YCMutablePhotoItemView
@interface YCMutablePhotoItemView ()<XHImageViewerDelegate>
@property (nonatomic,readonly) NSMutableArray *photoArray;// imageView数组
@end
@implementation YCMutablePhotoItemView
@synthesize photoArray = _photoArray;


- (instancetype)initWithArray:(NSArray *)array
{
    CGFloat height = 0;
    CGFloat width = 247.0 / 320 * kScreenWidth;
    
    _photoArray = [[NSMutableArray alloc]init];
    
    if (array.count == 0 || [array firstObject] == [NSNull null])
    {
        
    }
    else if (array.count == 1)
    {
        height = 160;
    }
    else if (array.count > 1 && array.count < 4)
    {
        height = 70;
    }
    else if (array.count > 3 && array.count < 7)
    {
        height = 150;
    }
    else
    {
        height = 230;
    }
    
    CGRect frame = CGRectMake(0, 0, width, height);
    
    if(self = [super initWithFrame:frame])
    {
        if (array.count == 0 || [array firstObject] == [NSNull null])
        {
            
        }
        else if (array.count == 1)
        {
            UIImageView * imageViews =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 160)];
            imageViews.userInteractionEnabled =YES;
            imageViews.contentMode = UIViewContentModeScaleAspectFill;
            imageViews.clipsToBounds = YES;
            UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
            [imageViews addGestureRecognizer:gesture];
            
            [imageViews yc_setWebImageWithURLString:[array firstObject] holderSize:CGSizeMake(160, 160)];
            [self addSubview:imageViews];
            [self.photoArray addObject:imageViews];
        }
        else if (array.count > 1 && array.count < 4)
        {
            for (int i = 0; i < array.count; i++)
            {
                UIImageView * imageViews =[[UIImageView alloc]initWithFrame:CGRectMake(80*i, 0, 70, 70)];
                imageViews.userInteractionEnabled =YES;
                imageViews.contentMode = UIViewContentModeScaleAspectFill;
                imageViews.clipsToBounds = YES;
                UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
                [imageViews addGestureRecognizer:gesture];
                
                [imageViews yc_setWebImageWithURLString:array[i] holderSize:CGSizeMake(70, 70)];
                [self addSubview:imageViews];
                [self.photoArray addObject:imageViews];
            }
        }
        else if (array.count > 3 && array.count < 7)
        {
            CGFloat viewHeight = 0;
            
            if (array.count == 4)
            {
                for (int i = 0; i < array.count/2; i++)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        UIImageView * imageViews =[[UIImageView alloc]initWithFrame:CGRectMake(80*j, 80*i, 70, 70)];
                        imageViews.userInteractionEnabled =YES;
                        imageViews.contentMode = UIViewContentModeScaleAspectFill;
                        imageViews.clipsToBounds = YES;
                        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
                        [imageViews addGestureRecognizer:gesture];
                        
                        [imageViews yc_setWebImageWithURLString:array[i*2+j] holderSize:CGSizeMake(70, 70)];
                        [self addSubview:imageViews];
                        [self.photoArray addObject:imageViews];
                    }
                }
            }
            else
            {
                for (int i = 0; i < array.count/3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        UIImageView * imageViews =[[UIImageView alloc]initWithFrame:CGRectMake(80*j, 80*i, 70, 70)];
                        imageViews.userInteractionEnabled =YES;
                        imageViews.contentMode = UIViewContentModeScaleAspectFill;
                        imageViews.clipsToBounds = YES;
                        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
                        [imageViews addGestureRecognizer:gesture];
                        
                        [imageViews yc_setWebImageWithURLString:array[i*3+j] holderSize:CGSizeMake(70, 70)];
                        [self addSubview:imageViews];
                        [self.photoArray addObject:imageViews];
                        
                        viewHeight = 80*(i+1);
                    }
                }
                
                for (int i = 0; i < array.count%3; i++)
                {
                    UIImageView * imageViews =[[UIImageView alloc]initWithFrame:CGRectMake(80*i, viewHeight, 70, 70)];
                    imageViews.userInteractionEnabled =YES;
                    imageViews.contentMode = UIViewContentModeScaleAspectFill;
                    imageViews.clipsToBounds = YES;
                    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
                    [imageViews addGestureRecognizer:gesture];
                    
                    [imageViews yc_setWebImageWithURLString:array[array.count-(array.count%3)+i] holderSize:CGSizeMake(70, 70)];
                    [self addSubview:imageViews];
                    [self.photoArray addObject:imageViews];
                }
            }
        }
        else
        {
            CGFloat viewHeight = 0;
            
            for (int i = 0; i < array.count/3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    UIImageView * imageViews =[[UIImageView alloc]initWithFrame:CGRectMake(80*j, 80*i, 70, 70)];
                    imageViews.userInteractionEnabled =YES;
                    imageViews.contentMode = UIViewContentModeScaleAspectFill;
                    imageViews.clipsToBounds = YES;
                    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
                    [imageViews addGestureRecognizer:gesture];
                    
                    [imageViews yc_setWebImageWithURLString:array[i*3+j] holderSize:CGSizeMake(70, 70)];
                    [self addSubview:imageViews];
                    [self.photoArray addObject:imageViews];
                    
                    viewHeight = 80*(i+1);
                }
            }
            
            for (int i = 0; i < array.count%3; i++)
            {
                UIImageView * imageViews =[[UIImageView alloc]initWithFrame:CGRectMake(80*i, viewHeight, 70, 70)];
                imageViews.userInteractionEnabled =YES;
                imageViews.contentMode = UIViewContentModeScaleAspectFill;
                imageViews.clipsToBounds = YES;
                UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
                [imageViews addGestureRecognizer:gesture];
                
                [imageViews yc_setWebImageWithURLString:array[array.count-(array.count%3)+i] holderSize:CGSizeMake(70, 70)];
                [self addSubview:imageViews];
                [self.photoArray addObject:imageViews];
            }
        }
    }
    return self;
}

- (CGFloat)heightWithArrayCount:(NSInteger)count
{
    CGFloat height = 0;
    if (count == 0)
    {
    }
    else if (count == 1)
    {
        height = 160;
    }
    else if (count > 1 && count < 4)
    {
        height = 70;
    }
    else if (count > 3 && count < 7)
    {
        height = 150;
    }
    else
    {
        height = 230;
    }
    return height;
}

// 点按手势处理
- (void)tapHandle:(UITapGestureRecognizer *)tap
{
    XHImageViewer *imageViewer = [[XHImageViewer alloc] init];
    imageViewer.delegate = self;
    [imageViewer showWithImageViews:self.photoArray selectedView:(UIImageView *)tap.view];
    
    UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
    longPressGr.minimumPressDuration = 1.0;
    [imageViewer addGestureRecognizer:longPressGr];
}

// 长按预览图片处理
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        XHImageViewer *imageViewer = (XHImageViewer *)gesture.view;
        
        UIImageView *imgView = imageViewer.currentView;
        if(!imgView) return;
        CGImageRef imageToDecode = imgView.image.CGImage; // Given a CGImage in which we are looking for barcodes
        
        ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode];
        ZXBinaryBitmap *bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];
        
        NSError *error = nil;
        ZXDecodeHints *hints = [ZXDecodeHints hints];
        
        ZXMultiFormatReader *reader = [ZXMultiFormatReader reader];
        ZXResult *result = [reader decode:bitmap hints:hints error:&error];
        
//        if (result)
//        {
//            NSString *contents = result.text;
//            NSString *scanStatusStr;
//            NSString *scanSupplierId;
//            if([contents hasPrefix:@"http://www.gonecn.com/newsoft/down.html"])
//            {
//                scanStatusStr = [contents go_paramOfName:@"type"];
//                scanSupplierId = [contents go_paramOfName:@"id"];
//                
//            }
//            else
//            {
//                NSArray *objs = [contents componentsSeparatedByString:@"#"];
//                scanStatusStr = objs.lastObject;
//                scanSupplierId = objs.firstObject;
//            }
//            [self loadData:scanStatusStr scanSupplierId:scanSupplierId];
//        }
//        else
//        {
//            [SVProgressHUD showErrorWithStatus:@"二维码图片不正确"];
//            return;
//        }
    }
}

//- (void)loadData:(NSString *)scanStatusStr scanSupplierId:(NSString *)scanSupplierId
//{
//    GOWebServiceSuccessCallBack success = ^(AFHTTPRequestOperation *operation, GOWebServiceResponse *response) {
//        if(response.success)
//        {
//            [SVProgressHUD showSuccessWithStatus:@"识别成功"];
//        }
//        else
//        {
//            [SVProgressHUD showErrorWithStatus:@"未获取到商家信息"];
//        }
//        
//    };
//    
//    GOWebServiceFailureCallBack failure = ^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//    };
//    
//    NSMutableDictionary *ps = [[NSMutableDictionary alloc] init];
//    NSString *action;
//    if ([scanStatusStr isEqualToString:@"shop"])
//    {
//        ps[@"suppliersId"] = scanSupplierId;
//        action = GOAPISuppliersGet;
//    }
//    else
//    {
//        ps[@"userId"] = scanSupplierId;
//        action = GOAPIUserUserinfoGet;
//    }
//    
//    [[GOWebService service] postAction:action
//                                params:ps
//                               success:success
//                               failure:failure];
//}




@end
